home *** CD-ROM | disk | FTP | other *** search
/ Delphi Magazine Collection 2001 / Delphi Magazine Collection 20001 (2001).iso / DISKS / ISSUE03 / RFEDIT2 / RFEDIT2.ZIP / RFEDIT.PAS < prev   
Encoding:
Pascal/Delphi Source File  |  1995-05-10  |  3.3 KB  |  143 lines

  1.  
  2. unit RFEdit;
  3.  {Implementation of a TEdit component with
  4.    filter and required field validation  }
  5. {Copyright (c), 1995 by Wm. Romano. All Rights Reserved}
  6. {Change Log :
  7.   May 10, 1995- Added Beep on Invalid property to allow control
  8.    of informing user when keying an invalid character. Jim Bandy}
  9. interface
  10.  
  11. uses
  12.   SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,
  13.   Forms, Dialogs, StdCtrls;
  14.  
  15. type
  16.   TValidChar   = String;
  17.   TRFilterEdit = class(TEdit)
  18.   private
  19.     { Private declarations }
  20.   protected
  21.   FRequired    : Boolean;
  22.   FBeepInvalid : Boolean;
  23.   FValidch     : TValidChar;
  24.   ValidChar    : set of char;
  25.     { Protected declarations }
  26.   Procedure SetValidCh(Value:TValidChar);
  27.   Procedure SetRequired(Value:Boolean);
  28.   Procedure SetBeepInvalid(Value:Boolean);
  29.   public
  30.     { Public declarations }
  31.   Constructor Create(AOwner:TComponent); override;
  32.   procedure KeyPress(var Key: Char); override;
  33.   Procedure DoExit; override;
  34.   published
  35.     { Published declarations }
  36.   Property ValidChars:TValidChar read FValidCh
  37.                                 Write SetValidCh;
  38.   Property RequiredField:Boolean read FRequired
  39.                                 write SetRequired;
  40.   Property BeepOnInvalid:Boolean read FBeepInvalid
  41.                                 write SetBeepInvalid;
  42.   end;
  43.  
  44. procedure Register;
  45.  
  46. implementation
  47.  
  48. Constructor TRFilterEdit.Create(AOwner:TComponent);
  49. begin
  50.   inherited Create(AOwner);
  51.   FValidCh:='[]';
  52.   FRequired:=False;
  53.   FBeepInvalid := False;
  54. end;
  55.  
  56. Procedure TRFilterEdit.SetRequired(Value:Boolean);
  57. Begin
  58.   if Value<>FRequired then
  59.     FRequired:=Value;
  60. end;
  61.  
  62. Procedure TRFilterEdit.SetBeepInvalid(Value:Boolean);
  63. Begin
  64.   if Value<>FBeepInvalid then
  65.     FBeepInvalid := Value;
  66. end;
  67.  
  68. procedure TRFilterEdit.DoExit;
  69. begin
  70.   inherited DoExit;
  71.   if FRequired then
  72.    begin
  73.      if Text = '' then
  74.      Begin
  75.        MessageDlg(' This field is required! ',mtWarning, [mbOK],0);
  76.        SetFocus;
  77.      end;
  78.    end;
  79. end ;
  80.  
  81. Procedure TRFilterEdit.SetValidCh(Value:TValidChar);
  82.   var
  83.   vc3 :  set of char;
  84.   x   :  integer;
  85.   ch1,ch2,ch3 : char;
  86.   SetRange    :  boolean;
  87.   {Internal Procedure}
  88.   Procedure MakeSet;
  89.   Begin
  90.     if SetRange then vc3:=[ch1..ch2]
  91.     else
  92.     vc3:=[ch1];
  93.     validchar:=validchar+vc3;
  94.     ch1:=#0;ch2:=#0;ch3:=#0;
  95.     SetRange:=False;
  96.   End;
  97.  
  98. Begin {SetValidCh}
  99.   SetRange:=False;
  100.   if Value<>FValidCh then
  101.    BEGIN
  102.      FValidCh:=Value;
  103.      if (FValidCh[1]='[') and
  104.        (FValidCh[length(FValidCh)]=']' )then
  105.      For x:=2 to length(FValidCh) do
  106.      begin
  107.        ch3:=FValidCh[x];
  108.        if (ch3=',') or (ch3=']') then
  109.        begin
  110.          MakeSet;
  111.        end;
  112.        if ch3='.' then SetRange:=True;
  113.        if ch3='''' then
  114.        begin
  115.          ch3:=FValidCh[x+1];
  116.          x:=x+2;
  117.        end;
  118.        if (SetRange=False) then ch1:=ch3
  119.        else ch2:=ch3;
  120.     end
  121.     else
  122.     FValidCh:='Invalid Format '+ FValidCh;
  123.   end;
  124. end;
  125.  
  126.  procedure TRFilterEdit.KeyPress(Var Key: Char);
  127.  begin
  128.    if ValidChar <>[] then
  129.     if (NOT (Key in ValidChar)) and (Key<>#08) then
  130.        begin
  131.          if FBeepInvalid then messagebeep(0);
  132.          Key:=#0;
  133.        end;
  134.    inherited KeyPress(Key);
  135.  end;
  136.  
  137. procedure Register;
  138. begin
  139.   RegisterComponents('Samples', [TRFilterEdit]);
  140. end;
  141.  
  142. end.
  143.